// TestKeys.cpp
//
#include <windows.h>

HINSTANCE hInst;										// global handle to hold the application instance
HWND wndHandle;											// global variable to hold the window handle

/******************************************************************************
* The window procedure
******************************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	// Check any available messages from the queue
	switch (message) {
	case WM_KEYDOWN:
		{
			HDC hdc = GetDC(hWnd);
			RECT rc = {0};
			GetClientRect(hWnd, &rc);
			FillRect(hdc, &rc, WHITE_BRUSH);
			TCHAR msg[256];
			int len = wsprintf(msg, TEXT("%d"), wParam);
			TextOut(hdc, 20, 20, msg, len);
			ReleaseDC(hWnd, hdc);
		}
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	// Always return the message to the default window
	// procedure for further processing
	return DefWindowProc(hWnd, message, wParam, lParam);
}

/******************************************************************************
* InitWindow registers the window class for the application, creates the window
******************************************************************************/
bool InitWindow(HINSTANCE hInstance, int nCmdShow)
{
	WNDCLASSEX wcex;
	// Fill in the WNDCLASSEX structure. This describes how the window will look to the system
	wcex.cbSize			= sizeof(WNDCLASSEX);					// the size of the structure
	wcex.style			= CS_HREDRAW | CS_VREDRAW;				// the class style
	wcex.lpfnWndProc	= WndProc;								// the window procedure callback
	wcex.cbClsExtra		= 0;									// extra bytes to allocate for this class
	wcex.cbWndExtra		= 0;									// extra bytes to allocate for this instance
	wcex.hInstance		= hInstance;							// handle to the application instance
	wcex.hIcon			= 0;									// icon to associate with the application
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);			// the default cursor
	wcex.hbrBackground	= reinterpret_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
	wcex.lpszMenuName	= NULL;									// the resource name for the menu
	wcex.lpszClassName	= TEXT("TestKeys");
	wcex.hIconSm		= 0;									// the handle to the small icon

	if (!RegisterClassEx(&wcex)) {
		MessageBox(NULL, TEXT("This program requires Windows NT!"), TEXT("TestKeys"), MB_ICONERROR);
		return false;
	}
	// Create the window
	wndHandle = CreateWindow(
		TEXT("TestKeys"),		// the window class to use
		TEXT("TestKeys"),		// the title bar text
		WS_OVERLAPPEDWINDOW,							// the window style
		CW_USEDEFAULT,									// the starting x coordinate
		CW_USEDEFAULT,									// the starting y coordinate
		640,											// the pixel width of the window
		480,											// the pixel height of the window
		NULL,											// the parent window; NULL for desktop
		NULL,											// the menu for the application; NULL for none
		hInstance,										// the handle to the application instance
		NULL											// no values passed to the window
	);

	// Make sure that the window handle that is created is valid
	if (!wndHandle) return false;

	// Display the window on the screen
	ShowWindow(wndHandle, nCmdShow);
	UpdateWindow(wndHandle);
	return true;
}

// This is winmain, the main entry point for Windows applications
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	// Initialize the window
	if (!InitWindow(hInstance, nCmdShow)) {
		MessageBox(NULL, TEXT("Cannot create the main window. Exiting."), TEXT("TestKeys"), MB_ICONERROR);
		return 1;
	}

	// main message loop:
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return static_cast<int>(msg.wParam);
}
